<?php

define('DS', DIRECTORY_SEPARATOR);
define('PS', PATH_SEPARATOR);
defined('MONK_PATH') || define('MONK_PATH', dirname(__FILE__));
define('CORE', MONK_PATH.DS.'Core'.DS);
define('SERVICES', MONK_PATH.DS.'Services'.DS);
defined('CONFIG_DRIVER') || define('CONFIG_DRIVER', 'Local');

class Monk{

    $_services = array();
    
    static public function S($name){
        $name = strtolower($name);
        if(isset($this->_services[$name]) && $this->_services[$name] instanceof IService) return $this->_services[$name];
        if(is_file(SERVICES.$name.DS.'Boot.php')){
            require(SERVICES.$name.DS.'Boot.php');
            try{
                return $this->_services[$name] = new 'Services_'.$name.'_Boot'(CONFIG_DRIVER);
            }catch{
                throw new MHttpException("Service {$name} cannot create.");
            }
        }
    }

    static public function autoload($className){
        S('Autoload')->load($className);
    }

}

spl_autoload_register(array('Monk','autoload'));
require(CORE.DS.'interfaces.php');